var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var serveStatic = require('serve-static');

var userRouter = require('./routes/user');
var chatRouter = require('./routes/chat');
var friendRouter = require('./routes/friend');
var commonRouter = require('./routes/commin');

var app = express();

app.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", '3.2.1')
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static('public', {
  'setHeaders': function (res, path) {
    res.setHeader("Content-type", "image/jpeg;");
  }
}));

app.get('/', function (req, res, next) {
  res.send('welcome my chatServer!');
});

app.use('/user', userRouter);
app.use('/chat', chatRouter);
app.use('/friend', friendRouter);
app.use('/common', commonRouter);

module.exports = app;
